package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //获取用户ID
        String password = employee.getPassword();
        //进行mD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //按姓名查询员工
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //获取此员工的真实对象
        Employee emp = employeeService.getOne(queryWrapper);
        //如果此员工对象为空说明没有此员工,响应"登陆失败"
        if (emp == null){
            return R.error("登陆失败");
        }
        //如果此员工输入的密码与数据库不匹配,响应"登陆失败"
        if (!emp.getPassword().equals(password)){
            return R.error("登陆失败");
        }
        //如果此员工为禁用状态,响应"账号已禁用"
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        request.getSession().setAttribute("employee",emp.getId());
        //如果以上条件都不符合,响应"success",表示登陆成功
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //移除登录ID
        request.getSession().removeAttribute("employee");
        //响应"退出成功"
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        //对新增员工进行mD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //添加到数据库
        employeeService.save(employee);
        //响应"新增员工成功"
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构建分页构造器
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //用name属性模糊查询员工数据,"StringUtils.isNotEmpty(name)"用来判断name属性是否为空
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //根据修改时间排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //根据员工ID查询员工对象数据
        Employee employee = employeeService.getById(id);
        //如果存在这个员工对象,响应给前端
        if(employee != null){
            return R.success(employee);
        }
        //如果不存在这个员工对象,响应给前端"没有查询到对应员工信息"
        return R.error("没有查询到对应员工信息");
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

}
